Skip to content

使用形式 ​

本节目标:理解“使用形式”的核心思路,并能把它用于实际开发或面试表达。

async 函数有多种使用形式。
`1`。 函数声明
`async` `function`

foo()


 `{`

} 2


。 函数表达式
`const` `foo` `=` `async` `function` `()` `{`

}; 3


。 箭头函数
`const` `foo` `=` `async` `()` `=>` `{` `};`

4。 对象的方法 let obj = { async

foo()

{ }

};
obj.foo().then(...)

5。 Class 的方法 class Storage {

constructor()

{

this.cachePromise

=

caches.open('avatars');
 `}`
`async`
getAvatar(name)

{const cache = await

this.cachePromise;
     `return`
cache.match(`/avatars/${name}.jpg`);
}
}
const

storage = new

Storage();
storage.getAvatar('jake').then(

…);